package com.ccnu.sm.jdbc;/*
 * @author raohj
 * @date 2019/11/1014:24
 * @description add your comment
 */
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;

@Service
public class RJdbcTemplate {
    private static final Log log = LogFactory.getLog(RJdbcTemplate.class);
    @Autowired
    private DataSource dataSource;

    private RCustDao rCustDao;

    public int test(){
        JdbcTemplate jdbcT = new JdbcTemplate();
        jdbcT.setDataSource(dataSource);
        rCustDao = new RCustDaoImpl(jdbcT);
        // 查询已提交记录
        RCust cust = rCustDao.selectById("20191104");
        log.info("执行查找完毕，查找到的值为:");
        if (cust == null) {
            log.info("null");
        } else {
            log.info(cust.toString());
        }

        // 查询所有记录
        log.info("查询所有库中的所有数据");
        for (RCust rCust : rCustDao.select()) {
            log.info(rCust.toString());
        }

        // 进行插入操作
        RCust custInsert = new RCust();
        custInsert.setCustNo("00001");
        custInsert.setCustName("贝贝");
        custInsert.setCustType("0");
        custInsert.setIdType("1");
        custInsert.setIdCode("4215646164616165");
        custInsert.setBankAcct("62137613464654654");
        rCustDao.insert(custInsert);
        log.info("执行插入完毕，插入后总记录数:" + rCustDao.selectCount());

        // 执行更新操作
        custInsert.setCustName("贝贝Darling");
        rCustDao.upate(custInsert);
        RCust custU = rCustDao.selectById(custInsert.getCustNo());
        log.info("执行更新操作后，从数据库里查回来的值为:");
        if (custU == null) {
            log.info("null");
        } else {
            log.info(custU.toString());
        }

        // 执行删除操作
        rCustDao.deleteById(custInsert.getCustNo());
        log.info("执行删除操作后，数据库中的记录数为:" + rCustDao.selectCount());

        return rCustDao.selectCount();
    }
}
